package com.project.web.intfc.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.yun.common.utils.redis.RedisClient;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 接口令牌拦截器
 *
 * @author WYY
 * @version 1.0
 * @date 2019/07/9
 */
public class InterfaceTokenInterceptor extends HandlerInterceptorAdapter {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "redisClient")
    private RedisClient redisClient;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //取出token
//        String headerToken = request.getHeader(WebConsts.INTERFACE_REQUEST_HEADER_TOKEN_KEY);
//        log.info("=======请求头中取出的Token:{}", headerToken);
//        //取出登陆账号
//        String headerAcctName = request.getHeader(WebConsts.INTERFACE_REQUEST_HEADER_ACCT_KEY);
//        log.info("=======请求头中取出的headerAcctName:{}", headerAcctName);
//        if (StringUtils.isBlank(headerToken) || StringUtils.isBlank(headerAcctName)) {
//            throw new CustomException(RESPONSE_CODE_ENUM.NOT_LOGIN_ERROR);
//        }
//        //获取redis中的token
//        Object redisToken = redisClient.get(RedisConsts.INTERFACE_USERID + headerAcctName);
//        if (redisToken == null) {
//            throw new CustomException(RESPONSE_CODE_ENUM.NOT_LOGIN_ERROR);
//        }
//        log.info("=======redis中取出的Token:{}", redisToken.toString());
//        //token不相同则重新登陆
//        if (!StringUtils.equals(headerToken, redisToken.toString())) {
//            throw new CustomException(RESPONSE_CODE_ENUM.TOKEN_IS_NOT_EXIST);
//        }
//        //重置token时间
//        redisClient.setAndExpire(RedisConsts.INTERFACE_USERID + headerAcctName, headerToken, RedisConsts.INTERFACE_USERID_EXPIRE);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }
}
